#include <iostream>
#include "ex03-library.h"
using namespace std;

// Do not modify
SongDatabase::SongDatabase() {
    this->songs.push_back("Penny Lane");
    this->songsInfo["Penny Lane"] = {"https://youtu.be/S-rB0pHI9fU", 8};

    this->songs.push_back("Trololo");
    this->songsInfo["Trololo"] = {"https://youtu.be/oavMtUWDBTM", 10};

    this->songs.push_back("Ob-La-Di, Ob-La-Da");
    this->songsInfo["Ob-La-Di, Ob-La-Da"] = {"https://youtu.be/_J9NpHKrKMw", 2};

    this->songs.push_back("Don't Worry, Be Happy");
    this->songsInfo["Don't Worry, Be Happy"] = {"https://youtu.be/d-diB65scQU", 3};

    this->songs.push_back("Leave My Kitten Alone");
    this->songsInfo["Leave My Kitten Alone"] = {"https://youtu.be/7BKsy9-Bvok", 5};
}

// Task 3(a).  Implement this method
// note: vector, map的访问
void SongDatabase::display() {
    // Write your code here
    for(size_t i; i < songs.size(); i++ ) {
        cout << "title=" << songs[i] <<
                "; url=" << songsInfo[songs[i]].url <<
                "; score=" << songsInfo[songs[i]].score << endl;
    }
}

// Task 3(b).  Implement this method
bool SongDatabase::addSong(string title, string url, unsigned int score) {
    // Write your code here
    if(score > 10) return false;
    map<string,Info>::iterator iter;
    iter = songsInfo.find(title);        // map.find(k) -> iterator
    if(iter != songsInfo.end()) {
        return false;
    } else {
        songs.push_back(title);
        songsInfo[title] = {url,score};
        return true;
    }
}

// Task 3(a).  Implement this method
void SongDatabase::searchSongs(string howGood) {
    // Write your code here
    map<string, Range> comments;
    comments["abysmal"] = {0,2};
    comments["lousy"] = {3,4};
    comments["meh"] = {5,6};
    comments["cool"] = {7,8};
    comments["OMG"] = {9,10};
    
    if(comments.find(howGood) == comments.end()) return;
    
    for(size_t i = 0; i < songs.size(); i++) {
        if( songsInfo[songs[i]].score >= comments[howGood].min &&
            songsInfo[songs[i]].score <= comments[howGood].max) {
            cout << "title=" << songs[i] <<
                "; url=" << songsInfo[songs[i]].url <<
                "; score=" << songsInfo[songs[i]].score << endl;
        }
    }

}
